<template>
<div style="width: 500px">
  <el-input
    placeholder="请输入部门名称"
    prefix-icon="el-icon-search"
    v-model="filterText">
  </el-input>
  <el-card class="box-card" style="margin-top: 10px;">
   <el-tree
      :data="depData"
      :props="defaultProps"
      :default-expand-all="true"
      :filter-node-method="filterNode"
      :expand-on-click-node="false"
      ref="tree">
      <span class="custom-tree-node" slot-scope="{ node, data }" style="display: flex;justify-content: space-between;width: 100%;">
        <span style="font-size: small">{{ data.name }} ({{data.children.length}})</span>
        <span>
          <el-button
            circle
            type="primary"
            size="mini"
            class="depBtn"
            icon="el-icon-plus"
            @click="() => showAddDep(data)">
          </el-button>
          <el-button
            circle
            type="danger"
            size="mini"
            class="depBtn"
            icon="el-icon-delete"
            @click="() => removeAddDep(data)">

          </el-button>
        </span>
      </span>
    </el-tree>
  </el-card>
  <el-dialog
    append-to-body
    title="添加部门"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="handleClose">
    <div>
      <table>
        <tr>
          <td>
            <el-tag>上级部门</el-tag>
          </td>
          <td style="margin-left: 100px">{{pname}}</td>
        </tr>
        <tr>
          <td>
            <el-tag>部门名称</el-tag>
          </td>
          <td>
            <el-input v-model="dep.name" placeholder="请输入部门名称" ></el-input>
          </td>
        </tr>
      </table>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="doAddDep">确 定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
import departAPI from "@/api/department";
export default {
  name: "DepMana",
  data(){
    return {
      filterText: '',
      depData:[],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      dialogVisible: false,
      dep:{
        name:'',
        parentId: -1
      },
      pname:'',// 上级部门名称
    }
  },
  methods:{
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    getDeps(){
      departAPI.getDepartment().then(res => {
        if (res){
          this.depData = res.data;
        }

      })
    },
    doAddDep(){
      departAPI.addDepartment(this.dep).then(res => {
        if (res){
          console.log("添加部門結果");
          console.log(res);
          this.dep.name = '';
          // 重新獲取部門信息
          this.getDeps();
          this.dialogVisible = false;
        }
      })
    },
    showAddDep(data){
      this.pname = data.name;
      this.dep.parentId = data.id;
      this.dialogVisible = true;
      console.log(data)
    },
    removeAddDep(data){
      this.$confirm('此操作将永久删除['+data.name+']部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 删除操作
        departAPI.delDepartment(data.id).then(res => {
          if (res){
            console.log("刪除部門結果");
            console.log(res);
            // 刷新部門信息
            this.getDeps();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    }
  },
  mounted() {
    this.getDeps();
  }
}
</script>

<style scoped>
.depBtn{
  padding: 2px;
}

</style>
